package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.ReservationsDao;
import com.ruoyi.reservationserver.entity.Reservations;
import com.ruoyi.reservationserver.service.ReservationsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 预约表(Reservations)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:49
 */
@Service("reservationsService")
public class ReservationsServiceImpl extends ServiceImpl<ReservationsDao,Reservations> implements ReservationsService {
    @Resource
    private ReservationsDao reservationsDao;

    @Resource
    private ReservationsService reservationsService;

    /**
     * 通过ID查询单条数据
     *
     * @param reservationId 主键
     * @return 实例对象
     */
    @Override
    public Reservations queryById(Integer reservationId) {
        return this.reservationsDao.queryById(reservationId);
    }

    /**
     * 分页查询
     *
     * @param reservations 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Reservations> queryByPage(Reservations reservations, PageRequest pageRequest) {
        long total = this.reservationsDao.count(reservations);
        return new PageImpl<>(this.reservationsDao.queryAllByLimit(reservations, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param reservations 实例对象
     * @return 实例对象
     */
    @Override
    public Reservations insert(Reservations reservations) {
        this.reservationsDao.insert(reservations);
        return reservations;
    }

    /**
     * 修改数据
     *
     * @param reservations 实例对象
     * @return 实例对象
     */
    @Override
    public Reservations update(Reservations reservations) {
        this.reservationsDao.update(reservations);
        return this.queryById(reservations.getReservationId());
    }

    /**
     * 通过主键删除数据
     *
     * @param reservationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer reservationId) {
        return this.reservationsDao.deleteById(reservationId) > 0;
    }

    @Override
    public PageInfo<Reservations> selectReservationWithPage(int page, int count) {
        PageHelper.startPage(page, count);

        QueryWrapper<Reservations> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("reservation_id");
        List<Reservations> reservationsList = reservationsService.list(queryWrapper);

        PageInfo<Reservations> pageInfo=new PageInfo<>(reservationsList);
        return pageInfo;
    }
}
